<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
	<meta name="format-detection" content="telephone=no" />
	<meta name="apple-mobile-web-app-capable" content="yes" />
	<meta name="apple-touch-fullscreen" content="yes">
	<meta content="telephone=no,email=no" name="format-detection" />
	<meta name="flexible" content="initial-dpr=2,maximum-dpr=3" />
	<!--去缓存-->
	<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
	<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
	<META HTTP-EQUIV="Expires" CONTENT="0"><!--end-->
	<title>羊仓健康生活家</title>
	<link rel="stylesheet" href="../css/gwcsubup.css" />
	<link rel="stylesheet" href="../css/style.css" />
	<script type="text/javascript" src="../js/flexible.js" ></script>
	<script type="text/javascript" src="../js/jquery.min.js" ></script>
	<script type="text/javascript" src="../js/basePathUtil.js" ></script>
	<!--弹出-->
	<script type="text/javascript" src="../js/layer-mobile/layer.js" ></script>
	<!--货币格式化-->
	<script type="text/javascript" src="../js/jquery-formatcurrency/jquery.formatCurrency.js" ></script>
	<script type="text/javascript" src="../js/Math.uuid.js" ></script>

	<script type="text/javascript">
		var basePath=basePath();
		var access_token = GetUrl("access_token");
	</script>

</head>
<body>
	<div class="warp">
		<!--地址-->
		<div class="top">
			<div class="topback">
				<img src="../img/red.png">
			</div>
			
			<!--<div class="topcont">-->
				<!--&lt;!&ndash;有地址是&ndash;&gt;-->
				<!--&lt;!&ndash;<div class="block">-->
					<!--<div class="contname">-->
						<!--<p class="name">栗子</p>-->
						<!--<div class="bor-radiu">-->
							<!--<p>默认</p>-->
						<!--</div>-->
					<!--</div>-->
					<!--<div class="contfamly">-->
						<!--<p class="phone">183103718166</p>-->
						<!--<p class="famly">北京市 朝阳区 奥市场价奥尼安静才是而</p>-->
					<!--</div>-->
				<!--</div>&ndash;&gt;-->
				<!--<div class="contipg">-->
					<!--<img src="../img/next.png" alt="">-->
				<!--</div>-->
				<!--&lt;!&ndash;添加地址是&ndash;&gt;-->
				<!--<div class="addres">-->
					<!--<div class="addimg">-->
						<!--<img src="../img/addre.png">-->
					<!--</div>-->
					<!--<p>添加收货地址</p>-->
				<!--</div>-->
			<!--</div>-->
		</div>
		<!--总价-->
		<!--<ul class="total">
			<li class="tocent">
				<div class="toleft">
					<p>商品合计</p>
				</div>
				<div class="toright">
					<p id="productTotalPrice">¥0.00</p>
				</div>
			</li>
			<li class="tocent">
				<div class="toleft">
					<p>运费</p>
				</div>
				<div class="toright">
					<p>¥0.00</p>
				</div>
			</li>
		</ul>-->
		<!--商品-->
		<ul class="ulshop" id="ulshop">
			<!--<li class="ullist">-->
				<!--<div class="listjpg">-->
					<!--<img src="../img/06.png">-->
				<!--</div>-->
				<!--<div class="listtxt">-->
					<!--<P class="listtit">珍惜天地天然营养富硒鸡蛋 年卡 30枚/每月*12</P>-->
					<!--<div class="listnum">-->
						<!--<P class="listmony">￥2370.00</P>-->
						<!--<p class="listnumber">x3</p>-->
					<!--</div>-->
				<!--</div>-->
			<!--</li>-->
			<!--<li class="ullist">-->
				<!--<div class="listjpg">-->
					<!--<img src="../img/06.png">-->
				<!--</div>-->
				<!--<div class="listtxt">-->
					<!--<P class="listtit">珍惜天地天然营养富硒鸡蛋 年卡 30枚/每月*12</P>-->
					<!--<div class="listnum">-->
						<!--<P class="listmony">￥2370.00</P>-->
						<!--<p class="listnumber">x3</p>-->
					<!--</div>-->
				<!--</div>-->
			<!--</li>-->
		</ul>
		<!--付款-->
		<div class="payment">
			<p class="patxt">实付：<span id="totalPrice">¥0.00</span><input type="hidden" id="subTotalPrice"/><span style="padding-left: 1rem; line-height:1.375rem ;">运费:￥0</span></p>
			<div class="paright">
                <input type="hidden" id="uuid"/>
				<button onclick="submitInfo()">付款</button>
			</div>
		</div>
	</div>
</body>
<script>
	function showAddress(address,result) {
		var sa='';
		if(result){
			sa+='<div class="topcont">';
			sa+='<div class="contname">';
			sa+='<input type="hidden" id="addressId" value="'+address.id+'"/>';
			sa+='<p class="name">'+address.contactName+'</p>';
			if(address.isDefault==1){sa+='<div class="bor-radiu"><p>默认</p></div>';}
			sa+='</div>';
			sa+='<div class="contfamly">';
			sa+='<p class="phone">'+address.contactMobile+'</p>';
			sa+='<p class="famly">'+address.address+' '+address.contactAddress+'</p>';
			sa+='</div>';
			sa+='<div class="contipg">';
			sa+='<img src="../img/next.png" alt="" onclick="chooseAddress()">';
			sa+='</div>';
			sa+='</div>';
		}else{
			sa+='<div class="topcont" onclick="chooseAddress()">';
			sa+='<input type="hidden" id="addressId" />';
			sa+='<div class="addres">';
			sa+='<div class="addimg">';
			sa+='<img src="../img/addre.png">';
			sa+='</div>';
			sa+='<p>请选择收货地址</p>';
			sa+='</div>';
			sa+='<div class="contipg">';
			sa+='<img src="../img/next.png" alt="" onclick="chooseAddress()">';
			sa+='</div>';
			sa+='</div>';
		}
		$(".top").append(sa);
	}
	function count() {
		//初始化数量和金额
		var totalPrice = 0,productTotalPrice=0;
		//判断是否为全选
		$("body .ulshop li").each(function(index){
//			alert($(this).find(".unitPrice").val());
//			alert($(this).find(".quantity").val());
//			alert($(this).find(".productId").val());
//			alert($(this).attr("id"));
			productTotalPrice+=parseFloat($(this).find(".unitPrice").val())*parseInt($(this).find(".quantity").val());
			//显示商品总额
			$("#productTotalPrice").text($.getFormattedCurrency(productTotalPrice));
			//显示应付总额，目前不涉及到运费，运费默认为0，取商品总金额即可
			$("#totalPrice").text($.getFormattedCurrency(productTotalPrice));
			$("#subTotalPrice").val(Math.round(productTotalPrice*100)/100);
		});
	}
	/**
	 * 提交订单
	 */
	function submitInfo() {
		var addressId = $("#addressId").val();
		if(addressId==null || addressId=="" || addressId<=0){
			layer.open({content: "请选择收货地址",skin: 'msg',time: 1});return;
		}
		if($("#uuid").val()==""){
			$("#uuid").val(Math.uuid());
		}
		//初始化数量和金额
		var totalPrice = 0,productTotalPrice=0,productList=new Array();
		//判断是否为全选
		$("body .ulshop li").each(function(index){
			var productObj = new Object();
			productObj.productId=parseInt($(this).find(".productId").val());
			productObj.quantity=parseInt($(this).find(".quantity").val());
			productObj.trolleyId=$(this).attr("id");
			productList.push(JSON.stringify(productObj));
		});
		var productJson = {};
		productJson.productList=productList;
		$.ajax({
			type:"POST",
			url:basePath+"/v1/trade/api/buy?access_token="+access_token,
			dataType : 'json',
			data: {
				totalPrice:$("#subTotalPrice").val(),
				productList:JSON.stringify(productJson),
				source:'WeChat',
				addressId:$("#addressId").val(),
				uuid:$("#uuid").val()
			},
			success:function(data){
				if(data.code==1000) {
					window.location.href=basePath+"res/html/gwcfs.html?access_token="+access_token+'&tradeId='+data.data.tradeId+"&t="+new Date().getTime();
				}else{
					layer.open({content: data.msg,skin: 'msg',time: 1});
				}
			},
			error:function(data) {
			}
		});
	}
	function chooseAddress() {
		var toUrl = basePath+"res/addressList.html?access_token="+access_token;
		if(GetUrl("id").length>0){
			toUrl+='&id='+GetUrl("id");
		}
		if(GetUrl("ids").length>0){
			toUrl+='&ids='+GetUrl("ids");
		}
		toUrl+="&t="+new Date().getTime();
		window.location.href= toUrl;
	}
	/**
	 * result true限购商品 数量显示1
	 * @param product
	 * @param result
	 */
	function item(product,result){
		var item = '';
		if(result){
			item+='<li class="ullist" id="">';
		}else{
			item+='<li class="ullist" id="'+product.id+'">';
		}
		item+='<div class="listjpg">';
		item+='<img src="'+product.bannerUrl+'" title="'+product.name+'">';
		item+='</div>';
		item+='<div class="listtxt">';
		item+='<p class="listtit">'+product.name+'</p>';
		item+='<div class="listnum">';
		item+='<p class="listmony">'+$.getFormattedCurrency(product.unitPrice)+'.00</p>';
		item+='<input type="hidden" class="unitPrice" value="'+product.unitPrice+'"/>';
		if(result){
			item+='<p class="listnumber">x1</p>';
			item+='<input type="hidden" class="quantity" value="1"/>';
			item+='<input type="hidden" class="productId" value="'+product.id+'"/>';
		}else{
			item+='<p class="listnumber">x'+product.num+'</p>';
			item+='<input type="hidden" class="quantity" value="'+product.num+'"/>';
			item+='<input type="hidden" class="productId" value="'+product.productId+'"/>';
		}
		item+='</div>';
		item+='</div>';
		item+='</li>';
		$("#ulshop").append(item);
	}
	function defaultAddress() {
		if(GetUrl("address").length>0){
			$.ajax({
				type:"GET",
				url:basePath+"/v1/user/address/details?access_token="+access_token,
				dataType : 'json',
				data : {
					id:GetUrl("address"),
					t:new Date().getTime()
				},
				success:function(data){
					if(data.code==1000) {
						showAddress(data.data,true);
					}else{
						layer.open({content: data.msg,skin: 'msg',time: 1});
					}
				},
				error:function(data) {
				}
			});
		}else{
			$.ajax({
				type:"GET",
				url:basePath+"/v1/user/address/getDefaultAddress?access_token="+access_token,
				dataType : 'json',
				data:{
					t:new Date().getTime()
				},
				success:function(data){
					if(data.code==1000) {
						showAddress(data.data,true);
					}else{
						//没有默认地址
						showAddress(data.data,false);
//					layer.open({content: data.msg,skin: 'msg',time: 1});
					}
				},
				error:function(data) {
				}
			});
		}
	}
	function trolleyShow() {
		//购物车计算，限购结算
		if(GetUrl("ids").length==0 && GetUrl("id").length==0){
			layer.open({content: '您未选中任何商品',skin: 'msg',time: 1});
			return;
		}
		//购物车结算
		if(GetUrl("ids").length>0 && GetUrl("id").length==0){
			$.ajax({
				type:"GET",
				url:basePath+"/v1/trolley/list?access_token="+access_token,
				dataType : 'json',
				data:{
					pageNum:1,
					pageSize:99,
					t:new Date().getTime()
				},
				success:function(data){
					if(data.code==1000) {
						//有数据
						if(data.data.list.length>0){
							var pids = GetUrl("ids").split(',');
							var counts = 0;
							$.each(data.data.list, function(index, content){
								$.each(pids, function(i, c){
									if(data.data.list[index].id==pids[i]){
										item(data.data.list[index],false);
										counts++;
									}
								});
							});
							if(counts==0){
								layer.open({
									content: "请在购物车中选择要结算的商品",
									skin: 'msg',
									time: 1,
									end: function () {
										window.location.href=basePath+"res/gwc.html?access_token="+access_token+"&t="+new Date().getTime();
									}
								});
							}
							count();
						}else{
							layer.open({
								content: "请在购物车中选择要结算的商品",
								skin: 'msg',
								time: 1,
								end: function () {
									window.location.href=basePath+"res/gwc.html?access_token="+access_token+"&t="+new Date().getTime();
								}
							});
						}
					}else{
						layer.open({content: data.msg,skin: 'msg',time: 1});
					}
				},
				error:function(data) {
				}
			});
			return;
		}
		//限购结算
		if(GetUrl("id").length>0 && GetUrl("ids").length==0){
			$.ajax({
				type:"GET",
				url:basePath+"/v1/merchandise/detail?access_token="+access_token,
				dataType : 'json',
				data:{
					pid:GetUrl("id"),
					t:new Date().getTime()
				},
				success:function(data){
					if(data.code==1000) {
						//有数据
						item(data.data,true);
						count();
					}else{
						layer.open({content: data.msg,skin: 'msg',time: 1});
					}
				},
				error:function(data) {
				}
			});
		}
	}
	defaultAddress();
	trolleyShow();
</script>
</html>
